package com.substation.ui.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

import com.substation.utils.SDCardUtils;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2018/6/15.
 */

public class BitmapDrawTextUtils {

    public static List<String> createWater(Context context, String sub, String unit, String time, List<String> paths) {
        List<String> results = new ArrayList<>();
        for (String path : paths) {
            results.add(drawTextToBitmap(context,path,sub,unit,time));
        }
        return results;
    }


    public static String drawTextToBitmap(Context gContext, String p, String sub, String unit, String time) {
        Resources resources = gContext.getResources();
        float scale = resources.getDisplayMetrics().density;
        Bitmap bitmap = BitmapFactory.decodeFile(p);

        android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
        // set default bitmap config if none
        if (bitmapConfig == null) {
            bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
        }
        // resource bitmaps are imutable,
        // so we need to convert it to mutable one
        bitmap = bitmap.copy(bitmapConfig, true);

        Canvas canvas = new Canvas(bitmap);
        // new antialised Paint
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        // text color - #3D3D3D
        paint.setColor(Color.rgb(255, 0, 0));
        // text size in pixels
        paint.setTextSize(bitmap.getWidth()/40*scale);
        // text shadow
        paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

        // draw text to the Canvas center
        Rect bounds1 = new Rect();
        Rect bounds2 = new Rect();
        Rect bounds3 = new Rect();
        paint.getTextBounds(sub, 0, sub.length(), bounds1);
        paint.getTextBounds(unit, 0, unit.length(), bounds2);
        paint.getTextBounds(time, 0, time.length(), bounds3);
        //int x = (bitmap.getWidth() - bounds.width()) / 2;
        //int y = (bitmap.getHeight() + bounds.height()) / 2;
        //draw  text  to the bottom
        int x = bitmap.getWidth() / 50;
        int z=bitmap.getHeight()-bitmap.getHeight()/50-(bounds1.height() +(bounds2.height()+bounds3.height())*2);
        int y=z+bounds1.height();
        canvas.drawText(sub, x, y, paint);
        y = z +bounds1.height() +bounds2.height()*2;
        canvas.drawText(unit,x,y,paint);
        y = z +bounds1.height() +(bounds2.height())*4;
        canvas.drawText(time,x,y,paint);
        String path = SDCardUtils.getSDCardPath() + System.currentTimeMillis() + ".jpg";
        try {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, new FileOutputStream(path));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return path;
    }


    /**
     * dip转pix
     *
     * @param context
     * @param dp
     * @return
     */
    private static int dp2px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }
}
